package com.nfit.shopweb.web.api;

import com.nfit.shopmodel.entity.product.Category;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import com.nfit.shopweb.service.CategoryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ClassName:CategoryController
 * Package:com.nfit.shopweb.web.api
 * Description
 *
 * @Author L
 * @Create 2024/12/9 9:56
 */
@Tag(name = "category",description = "商品分类管理")
@Controller
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/selectList/{parentId}")
    @ResponseBody
    public Result selectList(@PathVariable("parentId") Integer parentId) {
        //传递的是parentId(父分类id)
        List<Category> categories = categoryService.selectByParentIdTreeCategory(parentId);
        return Result.build(categories, ResultCodeEnum.SUCCESS);
    }

    /**
     * 导出：表示从数据库读数据到excel的过程是导出
     * @param response 响应对象
     */
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        categoryService.exportData(response);
    }

    /**
     * 导入功能
     * @param file 文件输入流
     * @param response 响应对象
     */
    @PostMapping("/excelUpload")
    @ResponseBody
    public Result excelUpload(MultipartFile file, HttpServletResponse response){
        categoryService.exportSave(file, response);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


}
